package com.souku.finance.application.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseTitleBarActivity;
import com.souku.finance.application.bean.RegisterBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.level.StringUtils;
import com.souku.finance.application.widget.titlebar.EaseTitleBar;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Register2Activity extends BaseTitleBarActivity implements View.OnClickListener {

    @BindView(R.id.title_bar)
    EaseTitleBar titleBar;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.iv_delet_phone)
    ImageView ivDeletPhone;
    @BindView(R.id.image2)
    ImageView image2;
    @BindView(R.id.et_invite_mobile)
    EditText etInviteMobile;
    @BindView(R.id.iv_delet_phone2)
    ImageView ivDeletPhone2;
    @BindView(R.id.image3)
    ImageView image3;
    @BindView(R.id.et_pass)
    EditText etPass;
    @BindView(R.id.iv_delet_pass)
    ImageView ivDeletPass;
    @BindView(R.id.image4)
    ImageView image4;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.iv_delet_code)
    ImageView ivDeletCode;
    @BindView(R.id.btn_code)
    Button btnCode;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.btn_register_agreement)
    TextView btnRegisterAgreement;
    @BindView(R.id.btn_login2)
    TextView btnLogin2;

    final CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            btnCode.setText(millisUntilFinished / 1000 + "S");
            btnCode.setEnabled(false);
        }

        @Override
        public void onFinish() {
            btnCode.setText("立即获取");
            btnCode.setEnabled(true);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register2);
        ButterKnife.bind(this);
    }

    @Override
    public void internet() {

    }

    @OnClick({R.id.btn_code, R.id.btn_login, R.id.btn_register_agreement, R.id.btn_login2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_code:
                String mobile = etMobile.getText().toString().trim();
                if (!StringUtils.isMobileNum(mobile)) {
                    ToastUtil.show("请输入手机号");
                    return;
                }
                sendCode(mobile);
                btnCode.setEnabled(false);
                timer.start();
                break;
            case R.id.btn_login:
                register();
                break;
            case R.id.btn_register_agreement:
                Intent intent = new Intent(this, RegisterAgreementActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_login2:
                finish();
                break;
        }
    }

    private void register() {
        String mobile = etMobile.getText().toString().trim();
        String inviteMobile = etInviteMobile.getText().toString().trim();
        String pass = etPass.getText().toString().trim();
        String code = etCode.getText().toString().trim();

        if (!StringUtils.isMobileNum(mobile)) {
            ToastUtil.show("请输入手机号");
            return;
        }
        if (pass.isEmpty()) {
            ToastUtil.show("请输入密码");
            return;
        }
        if (pass.length() > 20 || pass.length() < 8) {
            ToastUtil.show("密码长度8-20位");
            return;
        }
        if (code.isEmpty()) {
            ToastUtil.show("请输入验证码");
            return;
        }

        reqRegister(mobile, pass, inviteMobile, code);

    }

    public void reqRegister(String mobile, String pass, String inviteMobile, String code) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        map.put("userPwd", pass);
//        map.put("realName", realName);
//        map.put("idNo", idNo);
        map.put("sourceType", "1");
        map.put("sourceFrom", "APP");
        map.put("referer", "");
        map.put("code", code);
        map.put("invitedMobile", inviteMobile);
//        map.put("verifyToken", codeToken);
        map.put("pid", "");
        map.put("weiXinOpenId", "");
        RequestManager.getInstance().post(UrlConstact.REGISTER, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    RegisterBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), RegisterBean.class);
                    ShareUtil.getInstance()
                            .putExtra(Constant.IsRegistered, true)
                            .putExtra(Constant.Mobile, bean.getMobile())
                            .putExtra(Constant.Token, bean.getOauthToken())
                            .putExtra(Constant.UserId, bean.getId())
                            .putExtra(Constant.AvatarUrl, bean.getAvatarUrl())
                            .putExtra(Constant.IsNewHand, true)
                            .putExtra(Constant.IsReal, false)
                            .putExtra(Constant.IsRole, false)
                            .putExtra(Constant.IsSetPayPwd, false)
                            .putExtra(Constant.IsBind, bean.getIsBind())
                            .putExtra(Constant.IsBorrower, "1".equals(bean.getIsBorrower()))
                            .putExtra(Constant.IsLogin, true)
                            .putExtra(Constant.MemberLevel, bean.getMemberLevel())
                            .putExtra(Constant.CumulatedPoint, bean.getCumulatedPoint())
                            .putExtra(Constant.AvailablePoint, bean.getAvailablePoint())
                            .putExtra(Constant.MaxPoint, bean.getMaxPoint())
                            .putExtra(Constant.eAccountNum, bean.geteAccountNum())
                            .putExtra(Constant.eAccountBindTime, bean.geteAccountBindTime())
                            .putExtra(Constant.realName, bean.getRealName());
//                    ToastUtil.show("注册成功");
                    Intent intent = new Intent(Register2Activity.this, RegisterSucceedActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void sendCode(String mobile) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        RequestManager.getInstance().post(UrlConstact.SEND_MOBILE_CODE, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    ToastUtil.show("验证码发送成功");
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    @Override
    public void onClick(View view) {

    }
}
